package com.wxc.rbtree;

/**
 * @program: LoveDataStructures1
 * @description: [用一句话描述此类]
 * @author: wxc
 * @create: 2021-07-29 10:04
 **/
public class BalanceBinarySearchTree<E> extends BinarySearchTree<E> {
    // 左旋 右边高
    protected void rotateLeft(Node<E> node) {
        Node<E> childNode = node.right;
        Node<E> childLeftChildNode = childNode.left;
        node.right = childLeftChildNode;
        childNode.left = node;

        afterRotate(childLeftChildNode, childNode, node);
    }

    // 右旋
    protected void rotateRight(Node<E> node) {
        Node<E> childNode = node.left;
        Node<E> childRightChildNode = childNode.right;
        node.left = childRightChildNode;
        childNode.right = node;

        afterRotate(childRightChildNode, childNode, node);
    }

    protected void afterRotate(Node<E> childRightChildNode, Node<E> childNode, Node<E> node) {
        if (childRightChildNode != null) childRightChildNode.parent = node;
        childNode.parent = node.parent;


        if (node.parent == null) root = childNode;
        else if (node.isLeftChildNode()) node.parent.left = childNode;
        else if (node.isRightChildNode()) node.parent.right = childNode;

        node.parent = childNode;

    }
}
